package com.zhongfl.crm.lixiao.common;

import com.heptagram.lixiao.api.LiXiaoService;
import com.heptagram.lixiao.bean.request.revisitlog.LXRevisitLogListRequest;
import com.heptagram.lixiao.bean.response.revisitlog.LXRevisitLogResponse;
import com.heptagram.lixiao.common.exception.LiXiaoErrorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * LxRevisitLogListUtils 获取励销跟进记录工具类
 *
 * @author tiantian
 * @date 2018/9/20 20:03
 */
@Service
@Slf4j
public class LxRevisitLogListUtils {

    @Resource
    private LiXiaoService liXiaoService;

    /**
     * 查询条件
     */
    private static final int PAGE_SIZE = 100;

    public LXRevisitLogResponse findRevisitLogs(int pageIndex,boolean isInitData) throws LiXiaoErrorException {
        return this.findRevisitLogs(pageIndex,PAGE_SIZE,isInitData);
    }

    public LXRevisitLogResponse findRevisitLogs(int pageIndex,int pageSize, boolean isInitData) throws LiXiaoErrorException {
        LXRevisitLogListRequest request = new LXRevisitLogListRequest();
        if (!isInitData) {
            //时间范围没起作用？还是查全部
            String startDate = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            String endDate = LocalDate.now().plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            request.setStartDate(startDate);
            request.setEndDate(endDate);
        }
        request.setPageIndex(pageIndex);
        request.setPageSize(pageSize);

        return this.findRevisitLogs(request);
    }

    public LXRevisitLogResponse findRevisitLogs(LXRevisitLogListRequest lxRevisitLogListRequest) throws LiXiaoErrorException {
        LXRevisitLogResponse lxRevisitLogResponse = liXiaoService.getRevisitLogService().findRevisitLogs(lxRevisitLogListRequest);

        if (Objects.isNull(lxRevisitLogResponse)) {
            log.info("没有查询到最新的跟进记录");
            return null;
        }
        if (lxRevisitLogResponse.getData().getTotalCount() == 0) {
            log.info("没有查询到最新的跟进记录");
            return null;
        }

        return lxRevisitLogResponse;
    }

}
